package com.zygh.interactiveplatform.module.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.interactiveplatform.common.constant.EmployeeResponseCodeConst;
import com.zygh.interactiveplatform.common.domain.ResponseDTO;
import com.zygh.interactiveplatform.module.entity.UserRole;
import com.zygh.interactiveplatform.module.mapper.UserRoleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lee
 * @since 2021-01-28
 */
@Service
public class UserRoleService extends ServiceImpl<UserRoleMapper, UserRole> {
    @Resource
    private UserRoleMapper userRoleMapper;


    @Cacheable(value = "userRole", key = "'userRole'")
    public List<UserRole> findAllNoPage() {
        return userRoleMapper.selectList(null);
    }


    public ResponseDTO addUserRole(UserRole userRole) {
        int insert = userRoleMapper.insert(userRole);
        if (insert > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.DEPT_ROLEADD_EXIST);
    }


    public ResponseDTO updateUserRole(UserRole userRole) {
        int insert = userRoleMapper.updateById(userRole);
        if (insert > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.ROLE_UPDATE_EXIST);
    }


    public ResponseDTO deleteUserRole(int id) {
        int i = userRoleMapper.deleteById(id);
        if (i > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.ROLE_DELETE_EXIST);
    }


    public ResponseDTO findUserRoleAll(Integer current, Integer size) {
        List<UserRole> userRoles = userRoleMapper.selectList(null);
        if (userRoles != null) {
            PageHelper.startPage(current, size);
            PageInfo<UserRole> pageInfo = new PageInfo<>(userRoles);
            return ResponseDTO.succData(pageInfo);
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.EMPTY_INFORMATION_ERROR);
    }


}
